<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ForgetPasswordApiController extends AbstractRestfulController
{
    public function getList()
    {			
			
    }
    public function get($token)
    {
		header('Access-Control-Allow-Origin: *');
		$RetrivepasswordTable=$this->getServiceLocator()->get('Models\Model\RetrivePasswordFactory');
		$tokenExit=$RetrivepasswordTable->gettoken($token)->toarray();		
		if(count($tokenExit)!=0){		
			return new JsonModel(array(									
				'value' => '1',
			));
		}else{
			return new JsonModel(array(									
				'value' => '0',
			));
		}
    }
    public function create($s_eamil)
    {
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$RetrivepasswordTable=$this->getServiceLocator()->get('Models\Model\RetrivePasswordFactory');
		global $forgotPasswordSubject;
		global $frogotPasswordMessage;
		$email=$s_eamil['user_email'];			
		$module="http://localhost/pedia/pediahtmls/resetpassword.html?token=";
		$uniqueRecardemail = $userTable->checkUniqueRecord($email)->toarray();
		if(count($uniqueRecardemail)!=0){				
			$user_id=$uniqueRecardemail[0]['user_id'];
			$username=ucfirst($uniqueRecardemail[0]['user_name']);				
			$token = getUniqueCode('10');	
			$insForgotPassword = $RetrivepasswordTable->addForgetpwd($user_id,$email,$token);
			$frogotPasswordMessage = str_replace("<FULLNAME>","$username", $frogotPasswordMessage);
			$frogotPasswordMessage = str_replace("<PASSWORDLINK>",$module.$token, $frogotPasswordMessage);
			$to=$email;		
			echo $frogotPasswordMessage;exit;	
			if(sendMail($to,$forgotPasswordSubject,$frogotPasswordMessage)){
				$result = new JsonModel(array(					
					'output'    => 'success',					
					'value' 	=> 1,
					'message' 	=> 'email sent successfully',	
				));	
			}else{
				$result = new JsonModel(array(					
					'output' 	=> 'fail',
					'value' 	=> 0,
					'message'   => 'email sent failed',

				));	
			}
			
		}else{
			$result = new JsonModel(array(					
				'output' => 'fail',
				'value' => '0',
				'message' => 'Please enter valid email',
			));
		}
		return $result;
    }
    public function update($token, $pwd)
    {
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$RetrivepasswordTable=$this->getServiceLocator()->get('Models\Model\RetrivePasswordFactory');
		$tokenExit=$RetrivepasswordTable->gettoken($token);
		$token=$tokenExit->toarray();
		if(count($token)!=0){					
			$changepwd = $userTable->changepwd($token[0]['user_id'],$pwd['cnf_password']);			
			if($changepwd>=0){
				$userAuth=$userTable->updateUserRegAuth($token[0]['user_id']);
				$users=$userTable->userdetails($token[0]['user_id']);			
				$deletetokenid=$RetrivepasswordTable->deletetoken($token[0]['forget_pwd_id']);			
				$result = new JsonModel(array(					
					'output' => 'success',
					'value' => '1',
				));			
			}else{
				$result = new JsonModel(array(					
					'output' => 'fail',
					'value' => '0',
					'message' => 'password not updated please try again',
				));
			}
			return $result;	
		}else{
			return new JsonModel(array(					
				'output' => 'fail',
				'value' => '0',
				'message' => 'password not updated please try again',
			));
		}
    }
    public function delete($id)
    {
       
    }
}